#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QTextEdit>
#include<QMdiSubWindow>
#include<QMenu>
#include<QIcon>
#include<QKeySequence>
#include<QActionGroup>
#include<QToolButton>
#include"myaction.h"
#include<QSpinBox>
#include<QLabel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMenu* editMenu = ui->menubar->addMenu(tr("编辑(&E)"));
    editMenu->addSeparator();//添加一个分隔符
    QAction* action_Open = editMenu->addAction(QIcon(":/res/edit.png"),tr("打开文件(&O)"));
    action_Open->setShortcut(QKeySequence("Ctrl+O"));
    connect(action_Open,&QAction::triggered,this,&MainWindow::on_actionEdit_triggered);


    QActionGroup* group = new QActionGroup(this);
    QAction* action_L = group->addAction("左对齐(&L)");
    action_L->setCheckable(true);

    QAction* action_R = group->addAction("右对齐(&R)");
    action_R->setCheckable(true);


    QAction* action_C = group->addAction("居中(&C)");
    action_C->setCheckable(true);
    editMenu->addSeparator();

    editMenu->addAction(action_L);
    editMenu->addAction(action_R);
    editMenu->addAction(action_C);

    MyAction * action = new MyAction(this);
    editMenu->addAction(action);


    connect(action,&MyAction::getText,this,&MainWindow::slot_set_text);
    //toolBar是工具栏
    QToolButton* toolBtn = new QToolButton(this);
    toolBtn->setText(tr("颜色"));
    ui->toolBar->addWidget(toolBtn);

    QMenu* colorMenu = new QMenu(this);
    colorMenu->addAction(tr("红色"));
    colorMenu->addAction(tr("绿色"));
    colorMenu->addAction(tr("蓝色"));
    colorMenu->addAction(tr("黄色"));
    colorMenu->addAction(tr("黑色"));

    toolBtn->setMenu(colorMenu);

    toolBtn->setPopupMode(QToolButton::MenuButtonPopup);
    //添加分隔符
    ui->toolBar->addSeparator();

    QSpinBox* spinBox = new QSpinBox(this);
    ui->toolBar->addWidget(spinBox);

    //状态栏
    //临时状态信息
    ui->statusbar->showMessage(tr("欢迎使用多文档编辑器"),2000);//显示两秒

    //那如何永久显示呢
    //永久显示状态标签
    QLabel* permant = new QLabel(this);
    //
    //permant->setFrameStyle(QFrame::Box);
    permant->setFrameStyle(QFrame::Panel);
    permant->setText("mlf.club");
    ui->statusbar->addPermanentWidget(permant);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionshowdock_D_triggered()
{
    qDebug()<<"显示 dock widget";
    ui->dockWidget->show();

}


void MainWindow::on_actionNew_N_triggered()
{
    QTextEdit* textEdit = new QTextEdit(this);
    auto childWindow = ui->mdiArea->addSubWindow(textEdit);

    childWindow->setWindowTitle(tr("文本编辑子窗口"));
    childWindow->show();
}



void MainWindow::on_actionEdit_triggered()
{
    qDebug()<<"打开文件...";

}

void MainWindow::slot_set_text(const QString &str)
{
    ui->textEdit->setText(str);
}

